﻿using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using Microsoft.AspNetCore.Http;
using Simple.Services.System;
using Simple.Services.System.QHSEUser.Models;
using Simple.Services.System.QHSEViolationCategory.Models;

namespace Simple.Services;

public class QHSEKeyWorkSpecialtyService
{
    private readonly SimpleDbContext _context;
    private readonly CacheService _cacheService;
    public QHSEKeyWorkSpecialtyService(SimpleDbContext context, CacheService cacheService)
    {
        _context = context;
        _cacheService = cacheService;
    }
    public async Task<List<QHSEKeyWorkSpecialtyModel>> GetAsync()
    {
        string name = "947";
        var result = await _cacheService.GetQHSEViolationKeyWorkSpecialityAsync(name);
        if (result.Count == 0)
        {
            var workSpecialties = await _context.Set<QHSESysKeyWorkSpecialty>().AsNoTracking().OrderBy(u=>u.SpecialtyCode).ToListAsync();
            var models =  MapperHelper.Map<List<QHSEKeyWorkSpecialtyModel>>(workSpecialties);
            await _cacheService.SetQHSEViolationKeyWorkSpecialityAsync(models, name);
            return models;
        }
        else
        {
            var zzj = "sb";
        }
        return result;
    }
    public async Task<List<QHSEKeyWorkSpecialtyModel>> GetAsync(SimpleDbContext context)
    {
        string name = "947";
        var result = await _cacheService.GetQHSEViolationKeyWorkSpecialityAsync(name);
        if (result.Count == 0)
        {
            var workSpecialties = await context.Set<QHSESysKeyWorkSpecialty>().AsNoTracking().OrderBy(u => u.SpecialtyCode).ToListAsync();
            var models = MapperHelper.Map<List<QHSEKeyWorkSpecialtyModel>>(workSpecialties);
            await _cacheService.SetQHSEViolationKeyWorkSpecialityAsync(models, name);
            return models;
        }
        else
        {
            var zzj = "sb";
        }
        return result;
    }
    public async Task<string> GetCodeByItemAsync(string item)
    {
        var code = await _context.Set<QHSESysKeyWorkSpecialty>().AsNoTracking().Where(u=>u.Specialty.Equals(item)).Select(u=>u.SpecialtyCode).FirstOrDefaultAsync();
        return code;
    }
    public async Task<Dictionary<string, string>> GetDictionaryByItemAsync()
    {
        var dictionary = await _context.Set<QHSESysKeyWorkSpecialty>()
            .AsNoTracking()
            .Select(d => new { d.Specialty, d.SpecialtyCode }) // 只查询 Item 和 Code 字段
            .ToDictionaryAsync(d => d.Specialty, d => d.SpecialtyCode);
        return dictionary;
    }
    public async Task<string> GetItemByCodeAsync(string code)
    {
        var item = await _context.Set<QHSESysKeyWorkSpecialty>().AsNoTracking().Where(u => u.SpecialtyCode.Equals(code)).Select(u => u.Specialty).FirstOrDefaultAsync();
        return item;
    }
}
